var express = require('express');
var router = express.Router();
var ipInfo = require('ipip')();
var mongoose = require('mongoose');
var Log = require('../model/Log');
var Record = require('../model/Record');

global.data = [];
var returnRouter = function(io) {
    /* GET home page. */
    router.get('/', function(req, res, next) {
        res.render('index', {
            title: 'Express'
        });
    });


    var company = [{
        name: '广州万惠投资管理有限公司',
        ip: '14.18.142.17'
    }, {
        name: '北京玖富时代投资顾问有限公司',
        ip: '182.92.188.172'
    }, {
    //     name: '钱钱金融信息服务(北京)有限公司',
    //     ip: '47.89.41.99'
    // }, {
        name: '京金所(北京)信息技术有限公司',
        ip: '123.56.137.236'
    }, {
        name: '深圳中电金融服务有限公司',
        ip: '120.27.163.28'
    }, {
        name: '北京车之家信息技术有限公司',
        ip: '218.6.111.42'
    }, {
        name: '上海汇付数据服务有限公司',
        ip: '180.168.133.125'
    }, {
        name: '北京瑞钱宝资产管理服务有限公司',
        ip: '101.200.125.220'
    }, {
        name: '中国广核电力股份有限公司',
        ip: '210.83.228.8'
    }, {
        name: '中金农信',
        ip: '112.124.184.40'
    }, {
        name: '中富民信金融信息服务(上海)有限公司',
        ip: '61.155.222.221'
    }, {
        name: '冠群驰骋投资管理(北京)有限公司',
        ip: '118.26.162.227'
    }, {
        name: '珠海华润银行股份有限公司',
        ip: '112.95.153.199'
    }, {
        name: '民加科风信息技术有限公司',
        ip: '180.97.162.64'
    }, {
        name: '广东省贵金属交易中心有限公司',
        ip: '112.74.201.117'
    }, {
        name: '广州仲裁委员会',
        ip: '14.23.88.135'
    }, {
        name: '深圳前海金元宝互联网金融服务有限公司',
        ip: '121.201.8.145'
    }, {
        name: '北京乐租网络科技有限公司',
        ip: '123.56.140.222'
    }, {
        name: '胖胖猪信息咨询服务(北京)有限公司',
        ip: '182.92.80.173'
    }, {
        name: '信用宝金融信息服务(北京)有限公司',
        ip: '120.76.58.100'
    }, {
        name: '广州市人民政府法制办公室',
        ip: '59.41.8.215'
    }, {
        name: '中融民信资本管理有限公司',
        ip: '223.203.224.141'
    }, {
        name: '快快金融信息服务(上海)有限公司',
        ip: '180.97.161.250'
    }, {
        name: '重庆海尔小额贷款有限公司',
        ip: '27.223.99.195'
    }, {
        name: '北京凤凰信用管理有限公司',
        ip: '58.68.150.195'
    }, {
        name: '大连银行',
        ip: '59.46.191.35'
    }, {
        name: '积木盒子',
        ip: '218.6.110.132'
    }, {
        name: '网信有限公司',
        ip: '14.215.102.153'
    }, {
        name: '北京首创金融资产交易信息服务股份有限公司',
        ip: '111.207.174.77'
    }, {
        name: '深圳萨摩耶互联网金融服务有限公司',
        ip: '112.124.212.62'
    }, {
        name: '人人分期',
        ip: '118.186.219.219'
    }, {
        name: '北京恒昌利通投资管理有限公司',
        ip: '180.97.161.36'
    }, {
        name: '深圳市分期乐网络科技有限公司',
        ip: '119.29.47.33'
    }, {
        name: '人人行科技有限公司',
        ip: '183.6.246.16'
    }, {
        name: '蓝领贷',
        ip: '115.28.76.39'
    }, {
        name: '中信信托有限责任公司',
        ip: '219.141.248.70'
    }, {
        name: '聚金所(北京)资本管理有限公司',
        ip: '139.129.222.233'
    }, {
        name: '融泰浩元(北京)网络科技有限公司',
        ip: '61.155.222.211'
    }, {
        name: '北京闪银奇异科技有限公司',
        ip: '123.57.84.66'
    }, {
        name: '广州易贷金融信息服务股份有限公司',
        ip: '114.67.48.40'
    }, {
        name: '和信电子商务有限公司',
        ip: '218.75.177.193'
    }, {
        name: '北京领先创融网络科技有限公司',
        ip: '101.200.82.17'
    }, {
        name: '东亮拾财贷(厦门)资产管理有限公司',
        ip: '117.27.243.148'
    }, {
        name: '上海小雨金融信息服务有限公司',
        ip: '106.75.31.218'
    }, {
        name: '上海信而富企业管理有限公司',
        ip: '117.27.243.145'
    }, {
        name: '江西金格科技股份有限公司',
        ip: '115.29.235.188'
    }, {
        name: '深圳淘淘金互联网金融服务有限公司',
        ip: '119.29.117.15'
    }, {
        name: '富勤恒业科技发展（北京）有限公司',
        ip: '123.56.108.75'
    }, {
        name: '',
        ip: ''
    }];
    var ip = [
        '114.80.166.240',
        '125.93.76.70',
        '58.57.128.1',
        '59.37.38.2',
        '60.221.125.254',
        '60.213.66.60',
        '60.21.129.39',
        '60.190.127.21',
        '124.117.246.2',
        '219.135.103.13',
        '219.148.141.242',
        '211.139.80.112',
        '222.74.58.242',
        '59.108.49.35',
        '61.139.236.34',
        '221.226.5.122',
        '219.159.235.103',
        '218.64.55.222',
        '61.177.200.75',
        '59.61.50.102',
        '60.191.164.17',
        '218.22.9.56',
        '61.138.111.14',
        '61.185.29.9',
        '221.208.247.138',
        '219.148.69.21',
        '60.163.62.16',
        '118.72.63.200',
        '59.44.96.15',
        '221.239.19.84',
        '218.26.7.133',
        '221.2.162.220',
        '61.153.148.146',
        '61.185.57.219',
        '61.147.251.40',
        '61.132.88.36',
        '61.144.68.227',
        '123.182.163.23',
        '222.91.146.18',
        '180.153.163.209',
        '58.218.152.35',
        '222.174.54.10',
        '58.252.205.17'
    ];
    router.get('/sendData', function(req, res, next) {
        //console.log(io);

        var data = '{"timestamp": "12/Sep/2016:15:15:53 +0800", "fields": { "remote_addr": "10.10.10.213", "remote_user": "-", "body_bytes_sent": "383", "request_time": "0.000", "status": "200", "request": "GET /favicon.ico HTTP/1.1", "request_method": "GET", "http_referrer": "http://10.10.10.213/", "body_bytes_sent":"383", "http_x_forwarded_for": "-", "http_user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" } }';


        var json = JSON.parse(data);


        try {

            var size = new Number(company.length);
            var index = new Number(Math.random() * size).toFixed(0);
            if (index == company.length - 1) {
                var size = new Number(ip.length);
                var index = new Number(Math.random() * size).toFixed(0);
                json.fields.remote_addr = ip[index];
                json.city = ipInfo(ip[index]).city;
                json.type = '其他';
            } else {
                json.fields.remote_addr = company[index].ip;
                json.city = ipInfo(company[index].ip).city;
                json.type = '标准';
                json.fields.remote_user = company[index].name;
            }
            //console.log(json.city);
            // 查询 IP 信息，以字典格式返回
            //console.log(index +' '+ ip[index]+' '+ipInfo(ip[index]).city);
            var log = new Log({
                body_bytes_sent: json.fields.body_bytes_sent,
                city: json.city,
                http_referrer: json.fields.http_referrer,
                http_user_agent: json.fields.http_user_agent,
                http_x_forwarded_for: json.fields.http_x_forwarded_for,
                isSend: 0,
                remote_addr: json.fields.remote_addr,
                remote_user: json.fields.remote_user,
                request: json.fields.request,
                request_method: json.fields.request_method,
                request_time: json.fields.request_time,
                status: json.fields.status,
                timestamp: json.timestamp,
                type: json.type
            });
            log.save(function(err) {
                if (err) {
                    console.log(err);
                } else {
                    var record = Record.findOne({
                        city: json.city
                    }, function(err, record) {
                        if (record == null) {
                            record = new Record({
                                city: json.city,
                                count: 1
                            });
                            record.save();
                        } else {

                            Record.update({
                                _id: record._id
                            }, {
                                $set: {
                                    count: record.count + 1
                                }
                            }, function(err) {
                                //console.log(err);
                            });
                        }
                    });
                }
            });
        } catch (e) {
            console.log(index + ':' + e);
        }

        res.send('index');
    });

    return router;
}


module.exports = returnRouter;
